package com.demoworld;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by francis on 2017/6/14.
 * hash map 操作举例
 */
public class MapTest {
    private String key = "white-tiger-map";
    private String field = "white-tiger-field";
    private String value = "white-tiger";

    //添加单个元素 到 map， 查询单个元素
    @Test
    public void add() {
        CacheHelper.INSTANCE.delete(key);

        CacheHelper.INSTANCE.hset(key, field, value);
        String strValue = CacheHelper.INSTANCE.hget(key, field);
        Assert.assertEquals(value, strValue);
    }

    //批量操作
    @Test
    public void add_Muti() {
        CacheHelper.INSTANCE.delete(key);

        Map<String, String> map = new HashMap<>();
        map.put("1", "1");
        map.put("2", "2");
        map.put("3", "3");
        CacheHelper.INSTANCE.hmset(key, map);

        Map<String, String> tmpMap = CacheHelper.INSTANCE.hgetall(key);
        tmpMap.forEach((key, value) -> {
            System.out.println(String.format("key=%s, value=%s", key, value));
        });
    }


}
